package com.zengde.zxfy.controller;

import com.zengde.zxfy.common.Result;
import com.zengde.zxfy.common.PageBean;
import com.zengde.zxfy.dto.EmpDTO;
import com.zengde.zxfy.dto.EmpPageQueryDTO;
import com.zengde.zxfy.entity.Emp;
import com.zengde.zxfy.entity.EmpExpr;
import com.zengde.zxfy.service.EmpService;
import com.zengde.zxfy.service.FileService;
import jakarta.validation.Valid;
import net.sf.jsqlparser.util.validation.metadata.DatabaseException;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/emps")
public class EmpController {
    private final EmpService empService;
    private final FileService fileService;

    public EmpController(EmpService empService, FileService fileService) {
        this.empService = empService;
        this.fileService = fileService;
    }

    @GetMapping
    public Result<PageBean<Emp>> list(EmpPageQueryDTO empPageQueryDTO) {
        return Result.success(empService.list(empPageQueryDTO));
    }

    @PostMapping
    public Result<Map<String, Integer>> add(@Valid @RequestBody EmpDTO empDTO) {
        Map<String, Integer> data = null;
        try {
            // 将DTO转换为实体
            Emp emp = new Emp();
            emp.setUsername(empDTO.getUsername());
            emp.setName(empDTO.getName());
            emp.setGender(empDTO.getGender());
            emp.setPhone(empDTO.getPhone());
            emp.setPosition(empDTO.getPosition());
            emp.setSalary(empDTO.getSalary());
            emp.setAvatar(empDTO.getAvatar());
            
            // 转换日期字符串为LocalDate
            if (empDTO.getEntrydate() != null) {
                emp.setEntrydate(LocalDate.parse(empDTO.getEntrydate()));
            }
            emp.setDeptId(empDTO.getDeptId());
            
            // 设置工作经历
            if (empDTO.getWorkCompany() != null && empDTO.getWorkPosition() != null) {
                EmpExpr experience = new EmpExpr();
                experience.setCompany(empDTO.getWorkCompany());
                experience.setPosition(empDTO.getWorkPosition());
                if (empDTO.getWorkStartDate() != null) {
                    experience.setStartDate(LocalDate.parse(empDTO.getWorkStartDate()));
                }
                if (empDTO.getWorkEndDate() != null) {
                    experience.setEndDate(LocalDate.parse(empDTO.getWorkEndDate()));
                }
                emp.setExperiences(List.of(experience));
            }
            
            Integer id = empService.add(emp);
            data = new HashMap<>();
            data.put("id", id);
        } catch (DatabaseException de) {
            return Result.error("添加员工失败：" + "");
        } catch (Exception e) {
            return Result.error("添加员工失败：" + e.getMessage());
        }
        return Result.success(data);
    }

    @PutMapping
    public Result<Void> update(@RequestBody Emp emp) {
        empService.update(emp);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Integer id) {
        empService.delete(id);
        return Result.success();
    }

    @DeleteMapping("/batch")
    public Result<Void> deleteBatch(@RequestBody Map<String, List<Integer>> params) {
        empService.deleteBatch(params.get("ids"));
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<Emp> getById(@PathVariable Integer id) {
        return Result.success(empService.getById(id));
    }

    @PostMapping("/avatar")
    public Result<String> uploadAvatar(@RequestParam("file") MultipartFile file) {
        String url = fileService.upload(file);
        return Result.success(url);
    }
} 